package com.example.terminal.view.edit;


import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.terminal.R;
import com.example.terminal.adapter.DeviceListAdapter;
import com.example.terminal.base.BaseActivity;
import com.example.terminal.bean.DeviceListBean.RowsBean;
import com.example.terminal.bean.StocktakingDetailsBean;
import com.example.terminal.bean.StoreroomListBean;
import com.example.terminal.bean.TcheckDeviceListDataBean;
import com.example.terminal.global.Constant;
import com.example.terminal.http.NetworkUtils;
import com.example.terminal.http.network.OkGoBackListener;
import com.example.terminal.listener.OnItemClickListener;
import com.example.terminal.util.ToastUtils;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lzy.okgo.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

public class DeviceListDialogActivity extends BaseActivity {

    private RecyclerView mRvContainer;
    private EditText mEtDeviceName, mEt_DeviceModel;
    private TextView mTv_DeviceType, mTv_DeviceSearch, mTv_DeviceReset, mTv_DeviceCancel, mTv_DeviceSure;
    private ImageView mIv_DeviceType;
    private RelativeLayout mRl_DeviceType;

    private static EditorCallbackListener mEditorCallback;
    private static String mContent;
    private List<RowsBean> rowsBeanList;
    private DeviceListAdapter deviceListAdapter;
    private static int mId = 0;

    @Override
    protected int setContentView() {
        return R.layout.device_list_layout;
    }

    @Override
    protected void initView() {
        mRvContainer = findViewById(R.id.mRvContainer);

        mEtDeviceName = findViewById(R.id.mEt_DeviceName);//设备名称
        mEt_DeviceModel = findViewById(R.id.mEt_DeviceModel);//设备型号

        mRl_DeviceType = findViewById(R.id.mRl_DeviceType);//设备类型
        mTv_DeviceType = findViewById(R.id.mTv_DeviceType);//设备类型
        mIv_DeviceType = findViewById(R.id.mIv_DeviceType);//设备类型

        mTv_DeviceSearch = findViewById(R.id.mTv_DeviceSearch);//搜索按钮
        mTv_DeviceReset = findViewById(R.id.mTv_DeviceReset);//重置按钮

        mTv_DeviceSure = findViewById(R.id.mTv_DeviceSure);//确定按钮
        mTv_DeviceCancel = findViewById(R.id.mTv_DeviceCancel);//取消按钮

    }

    @Override
    protected void initData() {
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.BOTTOM);

        openKeyBoard(mEtDeviceName);
        openKeyBoard(mEt_DeviceModel);
    }

    @Override
    protected void initConnect() {
        HttpParams valueParams = new HttpParams();
        valueParams.put(Constant.STOREHOUSEID, String.valueOf(mId));

        NetworkUtils.getTerminalStockControlList(new OkGoBackListener((Activity) mActivity) {
            @Override
            public void onSuccess(Object body) {
                rowsBeanList = (List<RowsBean>) body;

                initAdapter();
            }
        }, valueParams);
    }

    private void initAdapter() {
        if (deviceListAdapter == null) {
            deviceListAdapter = new DeviceListAdapter(mActivity, rowsBeanList);
            mRvContainer.setAdapter(deviceListAdapter);
            mRvContainer.setLayoutManager(new LinearLayoutManager(mActivity));

            deviceListAdapter.setOnItemClickListener(new TakeItemClickListener());
//            deviceListAdapter.setOnTextClickListener(new TakeItemTextClickTextListener());
        } else {
            deviceListAdapter.updateAdapter(rowsBeanList);
        }
    }

    /**
     * 条目点击事件
     */
    private class TakeItemClickListener extends OnItemClickListener {
        @Override
        public void onClick(View view, int position) {
            rowsBeanList.get(position).setSelected(!rowsBeanList.get(position).isSelected());

            initAdapter();

            // 初始化选择
//            initSelected(getSelected());
        }
    }

    /**
     * 全选、全取消
     *
     * @param isSelected
     */
//    private void initSelected(boolean isSelected) {
//        mIvSelected.setImageDrawable(mActivity.getDrawable(isSelected ? R.mipmap.check_item_sel : R.mipmap.check_item_blue));
//    }

    /**
     * 判断是否全部选中
     *
     * @return
     */
//    private boolean getSelected() {
//        boolean isSelected = true;
//        for (int i = 0; i < mTakeDetailDataList.size(); i++) {
//            if (!mTakeDetailDataList.get(i).isSelected()) {
//                isSelected = false;
//                break;
//            }
//        }
//        return isSelected;
//    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mTv_DeviceSearch:

                break;
            case R.id.mTv_DeviceReset:

                break;
            case R.id.mTv_DeviceSure:
                List<StocktakingDetailsBean.DataBean.TcheckDeviceListBean> isSelectedData = getIsSelectedData();

                if (isSelectedData == null && isSelectedData.size() == 0) {
                    ToastUtils.showToast("请选择设备！");
                    return;
                }


                if (mEditorCallback != null)
                    mEditorCallback.onSubmit(isSelectedData);

                onFinish();
                break;
            case R.id.mTv_DeviceCancel:
                onFinish();
                break;
            case R.id.mRl_DeviceType:

                break;
            case R.id.mIv_DeviceType:

                break;
        }
    }

    private List<StocktakingDetailsBean.DataBean.TcheckDeviceListBean> getIsSelectedData() {
        List<StocktakingDetailsBean.DataBean.TcheckDeviceListBean> rowsBeans = new ArrayList<>();

        if (rowsBeanList != null && rowsBeanList.size() > 0) {
            for (int i = 0; i < rowsBeanList.size(); i++) {
                if (rowsBeanList.get(i).isSelected()) {
                    StocktakingDetailsBean.DataBean.TcheckDeviceListBean tcheckDeviceListBean = new StocktakingDetailsBean.DataBean.TcheckDeviceListBean();
                    tcheckDeviceListBean.setCheckNum(rowsBeanList.get(i).getResidueNum());
                    tcheckDeviceListBean.setDeviceCompany(rowsBeanList.get(i).getCompany());
                    tcheckDeviceListBean.setDeviceLeixing(rowsBeanList.get(i).getDeviceLeixinValue());
                    tcheckDeviceListBean.setDeviceName(rowsBeanList.get(i).getDeviceName());
                    tcheckDeviceListBean.setDeviceNo(rowsBeanList.get(i).getId());
                    tcheckDeviceListBean.setDeviceType(rowsBeanList.get(i).getDeviceType());
                    tcheckDeviceListBean.setImeiStatus(rowsBeanList.get(i).getImeiStatus());
                    rowsBeans.add(tcheckDeviceListBean);
                }
            }
        }

        return rowsBeans;
    }

    /**
     * 打开页面
     *
     * @param baseActivity
     * @param editorCallback
     */
    public static void openActivity(BaseActivity baseActivity, EditorCallbackListener editorCallback, int id) {
        Intent intent = new Intent(baseActivity, DeviceListDialogActivity.class);

        mEditorCallback = editorCallback;
        mId = id;

        baseActivity.turnActForResult(intent);
    }

    public interface EditorCallbackListener {
        void onSubmit(List<StocktakingDetailsBean.DataBean.TcheckDeviceListBean> tcheckDeviceListDataBeans);
    }

    @Override
    protected void changeSystemUi() {

    }

    @Override
    protected void onDestroy() {
        mEditorCallback = null;
        super.onDestroy();
    }
}
